package com.shudaolink.wms.print.controller;

import com.alibaba.fastjson.JSONArray;
import com.shudaolink.wms.print.servive.WmsPrintService;
import com.shudaolink.wms.print.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;

/**
 * @author : Sakura
 * @Date : 2024/4/16 10:14
 * @Description : TODO
 */
@Slf4j
@RestController
@RequestMapping("/print")
public class PrintController {

    // http://localhost:8082/print/reportPrint?url=C:/Users/Administrator/Desktop/temp/%E6%B5%8B%E8%AF%95%E6%89%93%E5%8D%B0.pdf&printerName=HPHP%20LaserJet%20Pro%204004%20UPD%20PCL%206
    @Resource
    private WmsPrintService printService;

    @Value(value = "${config.path}")
    private String path;

    /**
     * 一体机前端调用驱动打印机完成打印
     * @param url pdfUrl集合
     */
    @PostMapping("/reportPrint")
    public R<?> commonPrint(@RequestBody String url) throws Exception {
        InputStream inputStream = Files.newInputStream(Paths.get(path));
        Properties properties = new Properties();
        properties.load(inputStream);
        String printerName = properties.getProperty("print.name");
        System.out.printf(printerName);
        try {
            printService.commonPrint(url,printerName);
        } catch (Exception e) {
            log.info("打印失败,请求参数:"+ JSONArray.toJSONString(url));
            log.info(e.getMessage(),e);
            return R.error("打印失败,请联系管理员");
        }
        return R.success("打印成功");
    }

    @GetMapping("/getPrintName")
    public R<?> getPrintName() throws Exception {
        InputStream inputStream = Files.newInputStream(Paths.get(path));
        Properties properties = new Properties();
        properties.load(inputStream);
        String printerName = properties.getProperty("print.name");
        return R.success(printerName);
    }

}
